﻿using System;
using QD.Web;
using System.Web.UI;
using System.Text;
using System.IO;
using System.Web;
using WAD.DAL;
using System.Collections.Generic;

namespace WAD.WEB.AppCode
{
    public class PassportBase : WebPage
    {
        public int user_id;
        public string site_name;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            string userid = GetValue("userid");
            if (string.IsNullOrEmpty(userid))
                Response.Redirect("login.html");
            user_id = Convert.ToInt32(userid);
        }

       protected override void Render(HtmlTextWriter writer)
       {
           String msg = Toast();
           if (msg.Length == 0)
           {
               base.Render(writer);
               return;
           }

           StringBuilder sb = new StringBuilder();

           HtmlTextWriter HtmlWriter = new HtmlTextWriter(new StringWriter(sb));

           base.Render(HtmlWriter);

           //提示
           sb.Replace("</head>", msg + "</head>");

           writer.Write(sb.ToString());

       }
       protected string GetVersion(int id)
       {
           return WAD.BLL.VersionBLL.GetVersion(id);
       }


       protected void Echo(string msg, string url)
       {
           Session["toast_msg"] = msg;
           Response.Redirect(url);
       }

       /// <summary>
       /// 设置页面提示信息
       /// </summary>
       /// <param name="msg"></param>
       protected void SetToast(String msg)
       {
           Session["toast_msg"] = msg;
       }

       protected String Toast()
       {
           if (Session["toast_msg"] != null)
           {
               String msg = (String)Session["toast_msg"];
               Session["toast_msg"] = null;
               Session.Remove("toast_msg");
               return "<script>$(document).ready(function (){toast.show('" + msg + "');});</script>";
           }
           else
           {
               return "";
           }
       }

       protected string GetUName()
       {
           return GetValue("name");
       }

  

        protected bool IsAgent()
        {
            if (string.IsNullOrEmpty(GetValue("agentid")))
                return false;
            return true;
        }

        //设置代理商状态
        protected void SetAgent(string agentid)
        {
            SetValue("agentid", agentid);
        }

        protected string GetValue(string name)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
            if (cookie == null || string.IsNullOrEmpty(cookie.Value))
                return "";
            return Server.UrlDecode(cookie.Value);
        }

        protected void SetValue(string name, string value)
        {
            HttpCookie cookie = HttpContext.Current.Response.Cookies[name];
            if (cookie == null || string.IsNullOrEmpty(cookie.Value))
                cookie = new HttpCookie(name);
            cookie.Value = value;
            //cookie.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(cookie);
        }
    }
}